package com.yidiandao.business.ui.main.shoppingcart.chooseinvoice;

import com.yidiandao.business.data.model.IInvoiceModel;
import com.yidiandao.business.data.response.GetCartInvoiceResponse;

import java.util.List;

/**
 * 发票Presenter
 */
public class ChooseInvoicePresenter implements ChooseInvoiceContract.Presenter {

    private ChooseInvoiceContract.View mInvoiceView;

    private String mInvoiceType;

    private IInvoiceModel mInvoiceModel;

    private String mDefaultInvTitle;

    private List<String> mNormalInvoiceList;

    public ChooseInvoicePresenter(ChooseInvoiceContract.View view, IInvoiceModel invoiceModel) {
        mInvoiceView = view;
        mInvoiceModel = invoiceModel;

        mInvoiceView.setPresenter(this);
    }

    @Override
    public void start() {

        mInvoiceModel.getCartInvoices(mInvoiceView.getPagerTag(), new IInvoiceModel.OnGetCartInvoiceListListener() {
            @Override
            public void onSuccess(GetCartInvoiceResponse.Data data) {

                // 当前 默认的普通发票
                mDefaultInvTitle = data.getDefaultInvTitle();
                // 当前选择的发票类型
                String defaultInvType = data.getDefaultInvType();

                // 发票类型的列表
                List<String> invoiceTypeList = data.getInvoiceTypeList();
                mInvoiceView.refreshInvoiceType(invoiceTypeList);

                // 普通发票列表
                mNormalInvoiceList = data.getNormalInvoiceList();

                // 最后 再回显选择的发票
                mInvoiceView.changeInvoiceUIType(defaultInvType);
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public void onConfirmNoInvoice() {

        // 不传任何参数，不选择发票
        mInvoiceModel.changeInvoice(mInvoiceView.getPagerTag(), null, null, new IInvoiceModel.OnNetAccessListener() {
            @Override
            public void onSuccess() {

                mInvoiceView.finishActivity();
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public String getInvoiceType() {
        return this.mInvoiceType;
    }

    @Override
    public void setInvoiceType(String invoiceType) {
        mInvoiceType = invoiceType;
    }

    @Override
    public List<String> getNormalInvoiceList() {
        return mNormalInvoiceList;
    }

    @Override
    public String getDefaultNormalInvoice() {
        return mDefaultInvTitle;
    }
}
